REM -----------------------------------------------------------------------
REM $Id: archive.bat 11 2007-03-19 08:15:38Z beave $
REM -----------------------------------------------------------------------

@ECHO OFF

SET prog=archive.bat

SET tar_path=C:\Progra~1\GNU\tar\tar
SET zip_path=C:\Progra~1\GNU\zip\zip
SET rar_path=C:\Progra~1\WinRAR\rar
SET unzip_path=C:\Progra~1\GNU\unzip\unzip
SET unrar_path=C:\Progra~1\WinRAR\unrar

REM print incoming CGI variables
ECHO # CGI_HOME: %CGI_HOME%
ECHO # CGI_ARCHIVER: %CGI_ARCHIVER%
ECHO # CGI_COMPRESSOR: %CGI_COMPRESSOR%

REM change home directory if needed
IF DEFINED CGI_HOME (
	CD %CGI_HOME%
	ECHO # CD: %CD%
)

SET ACTION=%1%
SET ARCHIVE_PATH=%2%

REM print defined action and path
ECHO # ACTION: %ACTION%
ECHO # ARCHIVE_PATH: %ARCHIVE_PATH%

SET is_completed=0

IF DEFINED ACTION (
	GOTO %ACTION%
)

:help
ECHO ###################################################################
ECHO Usage.
ECHO -------------------------------------------------------------------
ECHO 1. add
ECHO %prog% add ARCHIVE_PATH 'ARCHIVE_CONTENT'
ECHO ---
ECHO 2. extract
ECHO %prog% extract ARCHIVE_PATH EXTRACT_TO
ECHO ---
ECHO 3. list
ECHO %prog% list ARCHIVE_PATH
ECHO ###################################################################
GOTO end



:add
SET ARCHIVE_CONTENT=%3%
ECHO # ARCHIVE_CONTENT: %ARCHIVE_CONTENT%

IF %CGI_ARCHIVER% == tar (
	SET is_completed=1
	IF EXIST %ARCHIVE_PATH% (
		%tar_path% --append --verbose --file=%ARCHIVE_PATH% %ARCHIVE_CONTENT%
	) ELSE (
		%tar_path% --create --verbose --file=%ARCHIVE_PATH% %ARCHIVE_CONTENT%
	)
)
IF %CGI_ARCHIVER% == zip (
	SET is_completed=1
	%zip_path% %ARCHIVE_PATH% %ARCHIVE_CONTENT%
)
IF %CGI_ARCHIVER% == rar (
	SET is_completed=1
	%rar_path% a -idq %ARCHIVE_PATH% %ARCHIVE_CONTENT%
)

IF %is_completed% == 0 (
	ECHO ERROR: unknown archiver
)
GOTO end



:extract

SET EXTRACT_TO=%3%
ECHO # EXTRACT_TO: %EXTRACT_TO%

IF %CGI_ARCHIVER% == tar (
	SET is_completed=1
	IF %CGI_COMPRESSOR% == gzip (
		%tar_path% -xzf %ARCHIVE_PATH% %EXTRACT_TO%
	) ELSE (
		IF %CGI_COMPRESSOR% == bzip2 (
			%tar_path% -xjf %ARCHIVE_PATH% %EXTRACT_TO%
		) ELSE (
			%tar_path% -xf %ARCHIVE_PATH% %EXTRACT_TO%
		)
	)
)
IF %CGI_ARCHIVER% == zip (
	SET is_completed=1
	IF DEFINED EXTRACT_TO (
		SET EXTRACT_PARAMS=-d %EXTRACT_TO%
	)
	%unzip_path% -o %ARCHIVE_PATH% %EXTRACT_PARAMS%
)
IF %CGI_ARCHIVER% == rar (
	SET is_completed=1
	%unrar_path% %ARCHIVE_PATH% %EXTRACT_TO%
)

IF %is_completed% == 0 (
	ECHO ERROR: unknown archiver
)
GOTO end



:list
IF %CGI_ARCHIVER% == tar (
	SET is_completed=1
	%tar_path% -tvf %ARCHIVE_PATH%
)
IF %CGI_ARCHIVER% == zip (
	SET is_completed=1
	%unzip_path% -l %ARCHIVE_PATH%
)
IF %CGI_ARCHIVER% == rar (
	SET is_completed=1
	%rar_path% a -idq l %ARCHIVE_PATH%
)

IF %is_completed% == 0 (
	ECHO ERROR: unknown archiver
)
GOTO end


:end
